package com.sfilyh.servelt.admin;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sfilyh.util.DBUtil;
import com.sfilyh.util.StringUtils;


@WebServlet("/bz-admin/addCategory")
public class addCategory extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 		String id = request.getParameter("id");
 		//String type = request.getParameter("type");
 		//request.setAttribute("type",type);
 		
 		if(StringUtils.validate(id)){
 			 request.setAttribute("doType", "update");
 			 Map<String, Object> categoryMap = DBUtil.queryForMap("select * from tb_category where id = ?", Integer.parseInt(id));
 			 request.setAttribute("category", categoryMap);
 			 request.setAttribute("type", categoryMap.get("type"));
 			 
 			 // 页面内容
 			 Object article = categoryMap.get("articleid");
 			 if(article != null){
 				request.setAttribute("article", DBUtil.queryForMap("select id,content from tb_article where id = ?", Integer.parseInt(article.toString())));
 			 }
 			 
 		}else{
 			request.setAttribute("doType", "add");
 		}
 		request.getRequestDispatcher("/WEB-INF/views/admin/addCategory.jsp").forward(request, response); 		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String cname = request.getParameter("cname");
		
		String sort = request.getParameter("sort");
		Integer sortInt = StringUtils.validate(sort)?Integer.parseInt(sort):null;
		
		String type = request.getParameter("type");
		Integer typeInt = StringUtils.validate(type)?Integer.parseInt(type):null;
		
		String content = request.getParameter("content");
		String dotype  = request.getParameter("dotype");
		String id = request.getParameter("id");
		String article = request.getParameter("article");
		
		if(StringUtils.validate(dotype,cname,type)){
			
		}
		
		if("add".equals(dotype)){
			// 分类 或 首页
 			if(type.equals("0") || type.equals("1")){
				String insertSql = "insert into tb_category(cname,type) values(?,?,?)";
				DBUtil.update(insertSql, cname,typeInt);				
			}else{
				// 页面
				String articleSql = "insert into tb_article(title,content,author,status) values(?,?,?,2)";
				int generateId =  DBUtil.updateReturnInsertId(articleSql,cname,content,1);
				
				String categorySql = "insert into tb_category(cname,sort,type,articleid) values(?,?,2,?)";
				int generatecid =  DBUtil.updateReturnInsertId(categorySql, cname,sortInt,generateId);
				
				String updateArticle = "update tb_article set category=? where id = ?";
				DBUtil.update(updateArticle, generatecid,generateId);				
			}
			
		}else if("update".equals(dotype)){
			
			String updateSql = "update tb_category set cname = ? where id = ?";
			DBUtil.update(updateSql, cname,id);
			
			// 更新页面内容
			if(StringUtils.validate(article)){		
				String updateArticle = "update tb_article set content = ?,title = ? where id = ? ";
				DBUtil.update(updateArticle, content,cname,Integer.parseInt(article));
			}				
			
		}
		
		response.sendRedirect(request.getContextPath()+"/bz-admin/listCategory");		
		
	}
		

}
